﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//using Microsoft.SqlServer.Management.Smo; // 2005
using Microsoft.SqlServer.Management.Sdk.Sfc; // 2008

namespace DBSourceToolsLib.SysObjects
{
    public class UrnParser
    {
        string m_Urn;
        SysObjectType m_Type;

        public UrnParser(Urn oUrn)
        {
            this.m_Urn = oUrn.ToString();
        }

        public string getTypeName()
        {
            if (this.m_Urn.Contains("/StoredProcedure"))
                m_Type = SysObjectType.Proc;
            if (this.m_Urn.Contains("/Table"))
                m_Type = SysObjectType.Table;
            if (this.m_Urn.Contains("/UserDefinedFunction"))
                m_Type = SysObjectType.Function;
            if (this.m_Urn.Contains("/UserDefinedDataType"))
                m_Type = SysObjectType.DataType;
            if (this.m_Urn.Contains("/View"))
                m_Type = SysObjectType.View;
            if (this.m_Urn.Contains("/XmlSchemaCollection"))
                m_Type = SysObjectType.XmlSchema;
            if (this.m_Urn.Contains("/UserDefinedTableType"))
                m_Type = SysObjectType.TableType;
            if (this.m_Urn.Contains("/DdlTrigger"))
                m_Type = SysObjectType.Trigger;
            if (this.m_Urn.Contains("/Synonym"))
                m_Type = SysObjectType.Synonym;
            

            return this.m_Type.ToString();
        }

        public string getObjectName()
        {
            ///StoredProcedure[@Name='uspUpdateEmployeeHireInfo' and @Schema='HumanResources']
            string strType = getUrnString();
            string objectNameAndSchema = this.m_Urn.Substring(m_Urn.IndexOf(strType) + strType.Length);
            string strObjectName1 = objectNameAndSchema.Substring(objectNameAndSchema.IndexOf("@Name='") + 7);
            string strObjectName = strObjectName1.Substring(0, strObjectName1.IndexOf("'"));
            string strSchemaName1 = objectNameAndSchema.Substring(objectNameAndSchema.IndexOf("@Schema='") + 9);
            string strSchemaName = strSchemaName1.Substring(0, strSchemaName1.IndexOf("'"));
            if (strSchemaName.Contains("\\")) // schema names may contain slashes i.e test\test1234
            {
                strSchemaName = strSchemaName.Replace('\\', '_');
            }
            if (this.m_Type == SysObjectType.Function)
            {
                return strSchemaName + "." + strObjectName;
            }
            if (strSchemaName.CompareTo("dbo") == 0)
                return strObjectName;
            else
                return strSchemaName + "." + strObjectName;
        }

        private string getUrnString()
        {
            switch (m_Type)
            {
                case SysObjectType.Table:
                    return "/Table";
                case SysObjectType.Proc:
                    return "/StoredProcedure";
                case SysObjectType.Function:
                    return "/UserDefinedFunction";
                case SysObjectType.DataType:
                    return "/UserDefinedDataType";
                case SysObjectType.View:
                    return "/View";
                case SysObjectType.XmlSchema:
                    return "/XmlSchemaCollection";
                case SysObjectType.TableType:
                    return "/UserDefinedTableType";
                case SysObjectType.Trigger:
                    return "/DdlTrigger";
                case SysObjectType.Synonym:
                    return "/Synonym";

            }
            return "";
        }
    }
}
